package com.inspur.dgov.vlbod;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import lombok.Data;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

/**
 * 代码生成器配置类
 *
 * @Author chiyupei
 * @Date 19/11/27 下午3:50
 * @Version 1.0
 * @Description 代码生成器配置类
 */
@Data
public class DgovCodeGenerator {

    public static void main(String[] args) {
        DgovCodeGenerator generator = new DgovCodeGenerator();
        generator.run();
    }

    public void run() {
        //数据源
        DataSourceConfig dsc = this.getDsConfig();

        //代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc);

        //全局配置
        GlobalConfig gc = this.getClobalConfig();
        mpg.global(gc);

        //包配置
        PackageConfig pc = new PackageConfig
                .Builder()
                .parent("com.inspur.dgov.vlbod")
                .moduleName(null)
                .build();
        mpg.packageInfo(pc);

        //配置模板
        TemplateConfig tc = new TemplateConfig
                .Builder()
                .build();
        mpg.template(tc);

        //策略配置
        StrategyConfig strategy = this.getStrategyConfig();
        mpg.strategy(strategy);

        mpg.execute(new FreemarkerTemplateEngine());
    }

    /**
     * 获取数据源
     * @return
     */
    private DataSourceConfig getDsConfig() {
        Properties props = getProperties();
        return new DataSourceConfig.Builder(
                props.getProperty("spring.datasource.url"),
                props.getProperty("spring.datasource.username"),
                props.getProperty("spring.datasource.password"))
                .typeConvert(new MySqlTypeConvert())
                .dbQuery(new MySqlQuery())
                .keyWordsHandler(new MySqlKeyWordsHandler())
                .build();
    }

    /**
     * 获取全局配置
     * @return
     */
    private GlobalConfig getClobalConfig() {
        return new GlobalConfig.Builder()
                .outputDir(getOutputDir())
                .author("chiyupei")
                .fileOverride()
                .openDir(false)
                .build();
    }

    /**
     * 获取策略配置
     * @return
     */
    private StrategyConfig getStrategyConfig() {
        return new StrategyConfig
                .Builder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .entityBuilder()
                .enableLombok()
                .enableSerialVersionUID()
                .idType(IdType.INPUT)
                .naming(NamingStrategy.underline_to_camel)
                .enableTableFieldAnnotation()
                .controllerBuilder()
                .enableRestStyle()
                .serviceBuilder()
                .formatServiceFileName("%sService")
                .mapperBuilder()
                .enableBaseResultMap()
                .enableBaseColumnList()
                .build();
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    /**
     * 获取配置文件
     *
     * @return 配置Props
     */
    private Properties getProperties() {
        // 读取配置文件
        Resource resource = new ClassPathResource("/templates/code.properties");
        Properties props = new Properties();
        try {
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    /**
     * 生成到项目中
     *
     * @return outputDir
     */
    public String getOutputDir() {
        return System.getProperty("user.dir") + "/src/main/java";
    }
}
